﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Xww.IO {
    /// <summary>
    /// 此类为生成临时文件提供帮助
    /// </summary>
    public class TempFileInfo {
        public string FilePath { get; private set; }
        protected FileInfo _FileInfo { get; private set; }
        ~TempFileInfo() {
            Delete();
        }
        /// <summary>Create an empty temp file.</summary>
        protected TempFileInfo() {
            FilePath = FilePathHelper.GenerateRandomFilePath();
            _FileInfo = new FileInfo(FilePath);
        }
        public TempFileInfo(IEnumerable<string> contents) : this() {
            if( contents != null )
                File.WriteAllLines(FilePath, contents.ToArray());
        }
        public TempFileInfo(string content) : this() {
            File.WriteAllText(FilePath, content);
        }
        public TempFileInfo(Action<StreamWriter> WriteAction)
            : this() {
            FilePath.WriteFileAction(WriteAction);
        }

        public void Delete() {
            if( _FileInfo.Exists )
                _FileInfo.Delete();
        }
    }
}
